﻿@namespace MudBlazor
@using MudBlazor.Interfaces
@inherits MudBaseButton

<MudElement HtmlTag="div" @attributes="UserAttributes" Class="@Classname" Style="@Style" 
     @onmouseenter="@(ActivationEvent== MouseEvent.MouseOver ? OpenMenu : null)"
     oncontextmenu="@(ActivationEvent == MouseEvent.RightClick ? "return false;" : null)">
    @if (ActivatorContent != null)
    {
        <CascadingValue Value="@((IActivatable) this)" IsFixed="true">
            <div @onmouseup="ToggleMenu">
                @ActivatorContent
            </div>
        </CascadingValue>
    }
    else if (String.IsNullOrEmpty(Icon))
    {
        <MudButton StartIcon="@StartIcon"
                   EndIcon="@EndIcon"
                   IconColor="@IconColor"
                   Color="@Color"
                   Size="@Size"
                   Variant="@Variant"
                   Disabled="@Disabled"
                   DisableRipple="@DisableRipple"
                   DisableElevation="@DisableElevation"
                   OnClick="@ToggleMenu">
            @Label
        </MudButton>
    }
    else
    {
        <MudIconButton Variant="@Variant" Icon="@Icon" Color="@Color" Size="@Size" Disabled="@Disabled" DisableRipple="@DisableRipple" @onclick="@ToggleMenu" />
    }
    @* The portal has to include  the cascading values inside, because it's not able to teletransport the cascade *@
    <MudPopover Open="@_isOpen" Class="@MenuClassname" MaxHeight="@MaxHeight" Style="@PopoverStyle" Direction="@Direction" OffsetX="@OffsetX" OffsetY="@OffsetY">
        <CascadingValue Value="@this">
            <MudList Clickable="true" Dense="@Dense" 
            @onmouseleave="@(ActivationEvent== MouseEvent.MouseOver ? CloseMenu : null)">
                @ChildContent
            </MudList>
        </CascadingValue>
    </MudPopover>
    <MudOverlay Visible="@(_isOpen && ActivationEvent!=MouseEvent.MouseOver)" OnClick="@ToggleMenu" LockScroll="LockScroll" />
</MudElement>


